package com.kucai.yifaqapp.ui.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.ResAdduser;
import com.kucai.yifaqapp.model.network.entity.res.ResCode;
import com.kucai.yifaqapp.util.Md5;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2018/12/20.
 */

public class RecommentFragmenttwo extends BaseFragmentSimple {
    EditText fusernameEt;
    EditText fnameEt;
    EditText fpasswordEt;
    EditText fpasswordEt2;
    EditText fqqnumberEt;
    EditText fphoneEt;
    EditText fcodeEt;
    TextView goneview;
    Button adduserbt;
    private ImageView code_img;
    private String codes;

    private String username;
    private String name;
    private String pass;
    private String pass2;
    private String phone;
    private String qqnumber;
    private String code;


    @Override
    protected int setContentView() {
        return R.layout.fragment_recomment_02;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getcode();
        code_img = view.findViewById(R.id.code_img1);
        fusernameEt = view.findViewById(R.id.fusername_et);
        fnameEt = view.findViewById(R.id.fname_et);
        fpasswordEt = view.findViewById(R.id.fpassword_et);
        fpasswordEt2 = view.findViewById(R.id.fpassword_et2);
        fqqnumberEt = view.findViewById(R.id.fqqnumber_et);
        fphoneEt = view.findViewById(R.id.fphone_et);
        fcodeEt = view.findViewById(R.id.fcode_et);
        adduserbt = view.findViewById(R.id.adduserbt);
        goneview = view.findViewById(R.id.goneview);
        code_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getcode();
            }
        });
        fqqnumberEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    goneview.setVisibility(View.VISIBLE);
                } else {
                    goneview.setVisibility(View.GONE);
                }
            }
        });
        fcodeEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    goneview.setVisibility(View.VISIBLE);
                } else {
                    goneview.setVisibility(View.GONE);
                }
            }
        });
        fphoneEt.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    goneview.setVisibility(View.VISIBLE);
                } else {
                    goneview.setVisibility(View.GONE);
                }
            }
        });
        adduserbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = fusernameEt.getText().toString().trim();
                name = fnameEt.getText().toString().trim();
                pass = fpasswordEt.getText().toString().trim();
                pass2 = fpasswordEt2.getText().toString().trim();
                qqnumber = fqqnumberEt.getText().toString().trim();
                phone = fphoneEt.getText().toString().trim();
                code = fcodeEt.getText().toString().trim();
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(getContext(), "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(getContext(), "请输入姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pass)) {
                    Toast.makeText(getContext(), "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(pass2)) {
                    Toast.makeText(getContext(), "请再次确认密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!pass.equals(pass2)) {
                    Toast.makeText(getContext(), "请确保两次密码一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(qqnumber)) {
                    Toast.makeText(getContext(), "请输入QQ号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(phone)) {
                    Toast.makeText(getContext(), "请输入手机号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    Toast.makeText(getContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
                    return;
                }

                adduserData();

            }
        });

    }

    /**
     * @Query("token") String token,
     * @Query("userName") String userName,
     * @Query("password") String password,
     * @Query("fullName") String fullName,
     * @Query("phone") String phone,
     * @Query("qq") String qq,
     * @Query("code") String code,
     * @Query("captcha_code") String captcha_code);
     */
    private void adduserData() {
        HttpJsonUtils.getHttpApiInstance()
                .adduser(HttpAdress.getuseAdress() + "api/m/pc/bet/insertUser",Info.user.token, username, Md5.toMd5(pass), name, phone, qqnumber, code, codes)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResAdduser>(this) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }

                    @Override
                    protected void onCustomNext(ResAdduser resCode) {
                        if (resCode.code == 0) {
                            Toast.makeText(getContext(), "添加成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getContext(), resCode.msg, Toast.LENGTH_SHORT).show();
                            getcode();
                        }
                    }
                });

    }

    @Override
    protected void ChangeSkin() {

    }

    public void getcode() {
        HttpJsonUtils.getHttpApiInstance()
                .getcode(HttpAdress.getuseAdress() + "api/m/captcha")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResCode>(this) {
                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {

                    }

                    @Override
                    protected void onCustomNext(ResCode resCode) {
                        if (null != resCode.data) {
                            codes = "";
                            codes = resCode.data.captcha_code;
                            Bitmap bitmap = null;
                            byte[] bitmapArray;
                            bitmapArray = Base64.decode(resCode.data.code, Base64.NO_WRAP);
                            bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
                            code_img.setImageBitmap(bitmap);//显示图片
                        }
                    }
                });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
}
